﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Entities;
using Eacademy.Domain.Models;
using Elmah;
using System.Transactions;
using SubSonic.DataProviders;

namespace Eacademy.Domain.Repository
{
    public class AssessmentRepository: IAssessmentRepository
    {
        EacademyDB db = new EacademyDB();
        public AssessmentViewModel GetQuestionsbyAssessmentId(int assessmentId)
        {
            assessmentId = 1;
            AssessmentViewModel assessmentViewModel = new AssessmentViewModel();
            try
            {
                List<AssessmentQuestion> question = db.Select.From<AssessmentQuestion>().Where(AssessmentQuestionsTable.AssessmentIdColumn)
                    .IsEqualTo(assessmentId).ExecuteTypedList<AssessmentQuestion>();
                List<AssessmentQuestionViewModel> questionList = new List<AssessmentQuestionViewModel>();
                
                foreach (AssessmentQuestion aq in question)
                {
                    AssessmentQuestionViewModel assessmentQuestionViewModel = new AssessmentQuestionViewModel();
                    assessmentQuestionViewModel.Question = aq.Question;
                    assessmentQuestionViewModel.QuestionId = aq.AssessmentQuestionId;
                    questionList.Add(assessmentQuestionViewModel);
                }
                assessmentViewModel.QuestionList = questionList;
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);            }
            return assessmentViewModel;
        }


        public AssessmentViewModel GetQuestionbyQuestionId(int id)
        {
            throw new NotImplementedException();
        }

        public AssessmentViewModel AddStudentRating(AssessmentViewModel assessmentViewModel)
        {
            try
            {
                int score = 0;
                foreach( var question in assessmentViewModel.QuestionList)
                {
                    AssessmentRating rating = new AssessmentRating();
                    using (TransactionScope ts = new TransactionScope())
                    {
                        using (SharedDbConnectionScope scs = new SharedDbConnectionScope())
                        {
                            rating.AssessmentStudentId = assessmentViewModel.StudentId;
                            rating.AssessmentQuestionId = question.QuestionId;
                            rating.Rating = question.Rating;
                            rating.Save();
                         }
                        ts.Complete();
                    }
                    score = score + question.Rating;
                }
                AssessmentStudent assessment = new AssessmentStudent();
                using (TransactionScope ts = new TransactionScope())
                {
                    using (SharedDbConnectionScope scs = new SharedDbConnectionScope())
                    {
                        assessment.AssessmentId = assessmentViewModel.AssessmentId;
                        assessment.StudentId = assessmentViewModel.StudentId;
                        assessment.MentorId = assessmentViewModel.MentorId;
                        assessment.SubjectId = assessmentViewModel.SubjectId;
                        assessment.Score = score;
                        assessment.Save();
                        }
                    ts.Complete();
                }
            return assessmentViewModel;
            }
        
            catch(Exception )
            {
            
            }
            return null;
        }
    }
}